/**
 * 
 *
 *    Code contributed by IUP Research Institute
 *     This file is part of NetworkMon.
 *
 *   NetworkMon is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License.
 *
 *   NetworkMon is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with NetworkMon; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package networkbackend;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import org.xbill.DNS.DClass;
import org.xbill.DNS.ExtendedResolver;
import org.xbill.DNS.Message;
import org.xbill.DNS.Name;
import org.xbill.DNS.Record;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.ReverseMap;
import org.xbill.DNS.Section;
import org.xbill.DNS.Type;

/**
 * 
 * @author Brian Slocum
 */
public class DNSLinkedList {

	Queue<DNSEntry> thequeue = new LinkedList<DNSEntry>();
	int queueSize;

	/** Creates a new instance of DNSLinkedList */
	public DNSLinkedList(int theQueueSize) {
		queueSize = theQueueSize;
	}

	public void AddDNSEntry(DNSEntry dnsEntry) {
		if (thequeue.size() > queueSize) {
			// remove an element the queue is to big
			thequeue.remove();
		}

		// add the element to the queue
		thequeue.add(dnsEntry);
	}

	public String reverseLookup(String theAddress) {
		DNSEntry currentNode = null;

		String retval = "";

		Iterator<DNSEntry> iterator = thequeue.iterator();

		while (iterator.hasNext()) {

			currentNode = (DNSEntry) iterator.next();

			if (currentNode != null) {
				if (currentNode.ipAddress.equals(theAddress) == true) {
					retval = currentNode.reverseAddress;
					return retval;
				}
			}
		}

		try {
			return DoreverseDns(theAddress);
		}

		catch (java.io.IOException ex) {
			System.out.println(ex.getMessage());
		}

		return "";
	}

	// this was taken from
	// http://www.oreillynet.com/onjava/blog/2005/11/reverse_dns_lookup_and_java.html
	public String DoreverseDns(String hostIp) throws java.io.IOException {

		// do not search becasue it is a MAC Address
		if (hostIp.indexOf(":") > 0) {
			return hostIp;
		}

		// this is the search routine
		Resolver res = new ExtendedResolver();

		Name name = ReverseMap.fromAddress(hostIp);
		int type = Type.PTR;
		int dclass = DClass.IN;
		Record rec = Record.newRecord(name, type, dclass);
		Message query = Message.newQuery(rec);
		Message response = res.send(query);

		Record[] answers = response.getSectionArray(Section.ANSWER);

		DNSEntry returnAddress = new DNSEntry(hostIp);

		if (answers.length == 0) {
			returnAddress.reverseAddress = hostIp;
			thequeue.add(returnAddress);

			return hostIp;
		}

		else {
			returnAddress.reverseAddress = answers[0].rdataToString();
			thequeue.add(returnAddress);
			return answers[0].rdataToString();
		}
	}

}
